/**
 * @format
 * @Author: TC
 * @Date: 2025-06-08 10:53:42
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-20 14:33:13
 */

import { DEBUG } from "cc/env";
import { app } from "db://assets/FrameWork/App";
import { nsCmdConst } from "db://assets/FrameWork/Module/InputCmd/CmdConst";
import { ICmdData } from "db://assets/FrameWork/Module/InputCmd/CmdInterface";
import { CmdItem } from "db://assets/FrameWork/Module/InputCmd/CmdItem";
import { EntityBase } from "../EntityBase";

export class EntityCmd {
    private _angle = 0;
    private _entity: EntityBase = null;
    public cmdItem: CmdItem = null; //输入命令
    public rdKey: Record<nsCmdConst.EKeyboardCode, boolean> = [] as any;
    // protected rdKeyDowned: Record<nsCmdConst.EKeyboardCode, boolean> = [] as any;
    public get entity(): EntityBase {
        return this._entity;
    }
    constructor(entity: EntityBase) {
        this._entity = entity;
        this.cmdItem = new CmdItem(this._entity, this.onCmd.bind(this));
    }

    public tick(): void {
        this.handleCmd();
    }

    /**
     * 输入命令回调
     * @param cmd
     */
    protected onCmd(cmd: ICmdData): void {
        // app.log.log(cmd.angle);
        switch (cmd.code) {
            case nsCmdConst.EKeyboardCode.MOVE: //遥感控制的移动
            case nsCmdConst.EKeyboardCode.LEFT:
            case nsCmdConst.EKeyboardCode.RIGHT:
            case nsCmdConst.EKeyboardCode.UP:
            case nsCmdConst.EKeyboardCode.DOWN:
            case nsCmdConst.EKeyboardCode.FOLLOW: //追踪(只有ai用到)
                // if (this.entity.act.isAttacking()) return;
                // break;
                this.entity.act.doMove(cmd.angle);
            case nsCmdConst.EKeyboardCode.FIRE:
                break;
            case nsCmdConst.EKeyboardCode.HIT:
                if (DEBUG) {
                    // this.entity.logic.hurt({ damage: 50000, critical: false });
                }
                break;
            default:
                this.entity.act.doIdle();
                break;
        }
    }

    private _h = 0;
    public get h(): number {
        return this._h;
    }
    public set h(h: number) {
        this._h = h;
    }
    private _v = 0;
    public get v(): number {
        return this._v;
    }
    public set v(v: number) {
        this._v = v;
    }

    public handleCmd(): void {
        let eCode = nsCmdConst.EKeyboardCode;
        let cmd: ICmdData = {
            code: eCode.IDLE,
            angle: -1,
        };

        if (this.rdKey[eCode.LEFT]) {
            cmd.code = eCode.LEFT;
        }
        if (this.rdKey[eCode.RIGHT]) {
            cmd.code = eCode.RIGHT;
        }
        if (this.rdKey[eCode.UP]) {
            cmd.code = eCode.UP;
        }
        if (this.rdKey[eCode.DOWN]) {
            cmd.code = eCode.DOWN;
        }
        if (this.rdKey[eCode.JUMP]) {
            cmd.code = eCode.JUMP;
        }
        if (this.rdKey[eCode.FIRE]) {
            cmd.code = eCode.FIRE;
        }
        if (this.rdKey[eCode.SKILL1]) {
            cmd.code = eCode.SKILL1;
        }
        if (this.rdKey[eCode.SKILL2]) {
            cmd.code = eCode.SKILL2;
        }
        if (this.rdKey[eCode.SKILL3]) {
            cmd.code = eCode.SKILL3;
        }
        if (this.rdKey[eCode.SKILL4]) {
            cmd.code = eCode.SKILL4;
        }
        if (this.rdKey[eCode.HIT]) {
            cmd.code = eCode.HIT;
        }
        if (this.rdKey[eCode.FOLLOW]) {
            cmd.code = eCode.FOLLOW;
        }
        if (this.rdKey[eCode.MOVE]) {
            cmd.code = eCode.MOVE;
        }
        this._h = Math.sign(this._h);
        this._v = Math.sign(this._v);
        cmd.angle = this.angle;
        this.cmdItem.inputCmd(cmd);
    }

    public set angle(a) {
        this._angle = a;
    }

    public get angle(): number {
        // console.log("angle", this._angle);
        if (this._angle > -1) {
            return this._angle;
        }
        return -1;
        let angle = -1;
        // if (this.entity.ai && this.entity.ai.following) {
        //     angle = this.entity.ai.getFollowAngle();
        // } else {
        if (0 == this._h) {
            if (0 == this._v) {
                angle = 0;
            } else if (-1 == this._v) {
                angle = 270;
            } else {
                angle = 90;
            }
        } else if (-1 == this._h) {
            if (0 == this._v) {
                angle = 180;
            } else if (-1 == this._v) {
                angle = 225;
            } else {
                angle = 135;
            }
        } else {
            if (0 == this._v) {
                angle = 0;
            } else if (-1 == this._v) {
                angle = 315;
            } else {
                angle = 45;
            }
        }
        // }

        return angle;
    }
}
